<?php

class LanguageController extends GTL_Action {

    public function init() {
        parent::init();
        $this->lng_id = '1';
       
        /* Initialize action controller here */
    }

    public function indexAction() {
       
        $this->_helper->redirector('form', 'language', 'default');
    }

    public function listAction(){
        
       
        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $orderField = 'ln.lang_id';
        $sort = 'asc';
        
        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;
        
        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }

        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
        $languageModel = new Model_Language();
       
        $languageData = $languageModel->fetchlanguages($where, $itemsPerPageReviews, $offset, $orderField, $sort);
       
        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        
        $paginator->setItemsTotal($languageModel->getLanguageCount($where));
           
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'language', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->languageData = $languageData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;

    }


    public function formAction() {

        $successmessage = "";
        $languageDocuments = "";
        $mem_description = "";
        $errorMessageArray = Array();
        $this->view->mode = "Add";
        $data = Array();
         
        $languageModel = new Model_Language();
        $languageForm = new Form_LanguageForm();
       
        
        $languageData = Array();
        $languageData['lang_id'] = $this->_request->getParam('lang_id');
        $lang_id = (!empty($languageData['lang_id'])) ? $languageData['lang_id'] : null;
  
        if (!empty($lang_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $languageModel->fetchEntryById($lang_id);
        }
        
         $this->view->form = $languageForm->populate($data);
         
         if ($this->getRequest()->isPost()) {
              //Check for Errors
            $postedData = $this->_request->getPost();
            if (!$languageForm->isValid($postedData)) {
                $errorMessage = $languageForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            }else{
                if (count($errorMessageArray) > 0) {
                    
                }else {
                 
                     
                           // $languageData['lang_name'] = $lang_id;
                            $languageData['lang_name'] = $postedData['lang_name'];
                            $languageData['lang_publish'] = 1;
                           
                            $languageModel->save($languageData, 'lang_id');
                               if ($this->view->mode == "Add") {
                                    $successmessage = 'Language add successfully';
                                } else {
                                    $successmessage = 'Language updated successfully';
                                }
                                $this->addSuccessMessage($successmessage);
                                $this->_helper->redirector('list', 'language', '');
                    
                }
            }
            
            
         }
        
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->mode = $this->view->mode;
        $this->view->form = $languageForm;
    }
    public function deleteAction() {

        $languageModel = new Model_Language();

        if ($this->_getParam('lang_id') && $this->_getParam('lang_id') != "") {
            $lang_id = $this->_getParam('lang_id');
            $languageData = Array();
            $languageData['lang_id'] = $lang_id;
            $languageData['lang_publish'] = '2';
            $languageModel->save($languageData, 'lang_id');

            
           
            $successmessage = "Language deleted successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('list', 'language', '');
            exit;
        }
    }

  
}

   
?>
